package com.dmx.cep.action.userManage;

import java.util.Date;

import org.apache.struts2.config.DefaultSettings;

import com.dmx.cep.entity.UserBaseInfo;
import com.dmx.cep.entity.UserInfo;
import com.dmx.cep.service.UserManageService;
import com.dmx.fw.constant.ViewConstant;
import com.dmx.fw.util.DateTimeUtils;
import com.dmx.fw.web.struts2.action.CommonQueryAction;


/**
 *
 * @author Alex.lu
 *
 */
@SuppressWarnings("serial")
public class UserBaseInfoAction extends CommonQueryAction {

    private static final String BATCH_DELETE = "batchDelete";
	private UserManageService userManageService;
	private UserBaseInfo userBaseInfo;
	private UserInfo userInfo;

	private String[] ids;
	private String id;
	private String q_uid;
	private String q_startDate;
	private String q_endDate;
	private String showCreateTime;
    private String q_birthday;
    private String q_createtime;
    private String q_email;
    private String q_familyid;
    private String q_flag;
    private String q_houseowner;
    private String q_income;
    private String q_jobCd;
    private String q_marriage;
    private String q_name;
    private String q_password;
    private String q_phone;
    private String q_relationship;
    private String q_safecode;
    private String q_sex;
    private String q_terminalid;
    private String language;


	// actions begin

	public String execute() throws Exception {
		super.busiId = "searchBaseUserInfo";
		if (ViewConstant.QUERY.equals(super.act)) {
			doQuery();
		} else if (ViewConstant.REQUERY.equals(super.act)) {
			doReQuery();
		} else if (ViewConstant.DELETE.equals(super.act)) {
			this.doDelete();
		} else if (BATCH_DELETE.equals(super.act)) {
			doBatchDelete();
		}else if (ViewConstant.ADDINPUT.equals(super.act)) {
			this.pageState = ViewConstant.ADDSTATUS;
			return doAddInput();
		} else if (ViewConstant.ADDSUBMIT.equals(super.act)) {
			return doAddSubmit();
		}else if (ViewConstant.EDITINPUT.equals(super.act)) {
			this.pageState = ViewConstant.EDITSTATUS;
			return doEditInput();
		} else if (ViewConstant.EDITSUBMIT.equals(super.act)) {
			return doEditSubmit();
		}else{
			super.execute();
		}
		return SUCCESS;
	}

	public String doAddInput() {
		
		return ViewConstant.ADDINPUT;
	}

	public String doAddSubmit() throws Exception {


		UserBaseInfo tempUserBaseInfo = userManageService.findUserBaseInfoById(userBaseInfo.getUid());
		if(tempUserBaseInfo!=null){
			super.setErrorInfo(super.getText("cep.userBaseInfo.warning.userid.duplicate"));
			return ViewConstant.ADDINPUT;
		}
        /**do your add logics**/
		userBaseInfo.setJobCd(null);
		userBaseInfo.setCreatetime(new Date());
		userManageService.saveOrUpdateUserBaseInfo(userBaseInfo);
		userInfo.setAge(3);
		userInfo.setFamilyid(userBaseInfo.getFamilyid());
		userInfo.setJobCd(null);
		userInfo.setSex(userBaseInfo.getSex());
		userInfo.setUid(userBaseInfo.getUid());
		userInfo.setHouseOwner(userBaseInfo.getHouseowner());
		userInfo.setUdJQ21Yourincome(userBaseInfo.getIncome());
		userInfo.setRelationship(userBaseInfo.getRelationship());
		userManageService.saveOrUpdateUserInfo(userInfo);
		
		Date date = userBaseInfo.getBirthday();
		if(date!=null){
			int ageType=0;
			int age =DateTimeUtils.getAgeByDate(date);
			if(age<=6){
				ageType=1;
			}else if(age>6&&age<=17){
				ageType=2;
			}else if(age>17&&age<=40){
				ageType=3;
			}else if(age>40&&age<=65){
				ageType=4;
			}else if(age>65){
				ageType=5;
			}
			userInfo.setAge(ageType);
		}
		setSuccessinfo(getText("info.addSuccess"));
		super.doReQuery();
		return SUCCESS;
	}

	public String doEditInput() {
		if (this.id != null) {
			this.pageState = "edit";
			userBaseInfo = userManageService.findUserBaseInfoById(id);
			userInfo = userManageService.findUserInfoById(id);

		}
		return ViewConstant.EDITINPUT;
	}
	
	

	public String doEditSubmit() throws Exception {


//		if (someerrorcondition) {
//			super.setErrorInfo(getText("m3p.asset.distribution.policy.required.warning"));
//			this.pageState = "edit";
//			return ViewConstant.ADDINPUT;
//		}
        /**do your update logics**/
		userBaseInfo.setJobCd(null);
		userManageService.saveOrUpdateUserBaseInfo(userBaseInfo);
		userInfo.setAge(3);
		userInfo.setFamilyid(userBaseInfo.getFamilyid());
		userInfo.setJobCd(null);
		userInfo.setSex(userBaseInfo.getSex());
		userInfo.setUid(userBaseInfo.getUid());
		userInfo.setHouseOwner(userBaseInfo.getHouseowner());
		userInfo.setUdJQ21Yourincome(userBaseInfo.getIncome());
		userInfo.setRelationship(userBaseInfo.getRelationship());
		userManageService.saveOrUpdateUserInfo(userInfo);
		Date date = userBaseInfo.getBirthday();
		if(date!=null){
			int ageType=0;
			int age =DateTimeUtils.getAgeByDate(date);
			if(age<=6){
				ageType=1;
			}else if(age>6&&age<=17){
				ageType=2;
			}else if(age>17&&age<=40){
				ageType=3;
			}else if(age>40&&age<=65){
				ageType=4;
			}else if(age>65){
				ageType=5;
			}
			userInfo.setAge(ageType);
		}
		setSuccessinfo(getText("info.addSuccess"));
		super.doReQuery();
		return SUCCESS;
	}

	public String doBatchDelete() throws Exception {
		userManageService.batchDeleteUserBaseInfoByIds(ids);
		userManageService.batchDeleteUserInfoByIds(ids);
		return super.doQuery();
	}


	public String doDelete() throws Exception {
		userManageService.deleteUserInfoById(id);
		userManageService.deleteUserBaseInfoById(id);
		setSuccessinfo(getText("info.deleteSuccess"));
		return super.doQuery();
	}



	// actions end

	// getters and setters begin





	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setUserManageService(UserManageService userManageService) {
		this.userManageService = userManageService;
	}

	public UserBaseInfo getUserBaseInfo() {
		return this.userBaseInfo;
	}
	
	public void setUserBaseInfo (UserBaseInfo userBaseInfo) {
		this.userBaseInfo = userBaseInfo;
	}


	
	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public String getQ_birthday() {
		return this.q_birthday;
	}

	public void setQ_birthday(String birthday) {
		this.q_birthday = birthday;
	}
	public String getQ_createtime() {
		return this.q_createtime;
	}

	public void setQ_createtime(String createtime) {
		this.q_createtime = createtime;
	}
	public String getQ_email() {
		return this.q_email;
	}

	public void setQ_email(String email) {
		this.q_email = email;
	}
	public String getQ_familyid() {
		return this.q_familyid;
	}

	public void setQ_familyid(String familyid) {
		this.q_familyid = familyid;
	}
	public String getQ_flag() {
		return this.q_flag;
	}

	public void setQ_flag(String flag) {
		this.q_flag = flag;
	}
	public String getQ_houseowner() {
		return this.q_houseowner;
	}

	public void setQ_houseowner(String houseowner) {
		this.q_houseowner = houseowner;
	}
	public String getQ_income() {
		return this.q_income;
	}

	public void setQ_income(String income) {
		this.q_income = income;
	}
	public String getQ_jobCd() {
		return this.q_jobCd;
	}

	public void setQ_jobCd(String jobCd) {
		this.q_jobCd = jobCd;
	}
	public String getQ_marriage() {
		return this.q_marriage;
	}

	public void setQ_marriage(String marriage) {
		this.q_marriage = marriage;
	}
	public String getQ_name() {
		return this.q_name;
	}

	public void setQ_name(String name) {
		this.q_name = name;
	}
	public String getQ_password() {
		return this.q_password;
	}

	public void setQ_password(String password) {
		this.q_password = password;
	}
	public String getQ_phone() {
		return this.q_phone;
	}

	public void setQ_phone(String phone) {
		this.q_phone = phone;
	}
	public String getQ_relationship() {
		return this.q_relationship;
	}

	public void setQ_relationship(String relationship) {
		this.q_relationship = relationship;
	}
	public String getQ_safecode() {
		return this.q_safecode;
	}

	public void setQ_safecode(String safecode) {
		this.q_safecode = safecode;
	}
	public String getQ_sex() {
		return this.q_sex;
	}

	public void setQ_sex(String sex) {
		this.q_sex = sex;
	}
	public String getQ_terminalid() {
		return this.q_terminalid;
	}

	public void setQ_terminalid(String terminalid) {
		this.q_terminalid = terminalid;
	}

	public String getQ_startDate() {
		return q_startDate;
	}

	public void setQ_startDate(String date) {
		q_startDate = date;
	}

	public String getQ_endDate() {
		return q_endDate;
	}

	public void setQ_endDate(String date) {
		q_endDate = date;
	}
	
	public String getShowCreateTime() {
		return showCreateTime;
	}
	// getters and setters end

	public void setQ_uid(String q_uid) {
		this.q_uid = q_uid;
	}

	public String getQ_uid() {
		return q_uid;
	}

	public String getLanguage() {
		language =DefaultSettings.get("struts.locale");
		return language;
	}
}